package com.defangit.production.orm;

import com.defangit.production.bll.WorkShopBll;
import com.defangit.production.domain.WorkShop;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Param;

public class WorkShopService extends CommonService<WorkShop> implements WorkShopBll {
    public WorkShop addWorkShop(WorkShop workShop){
        add(workShop);
        commitSession();
        return get(workShop.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("车间实体信息异常"));
    }
    public WorkShop updateWorkShop(WorkShop workShop){
        update(workShop);
        commitSession();
        return get(workShop.getId()).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("车间实体信息异常"));
    }

    @Override
    public boolean deleteWorkShop(int id) {
        return false;
    }

    @Override
    public WorkShopFinder findAll() {
        return new WorkShopFinderImp();
    }

    @Override
    public WorkShopFinder findUsed() {
        return new WorkShopFinderImp().workShopUsed(false);
    }

    public class WorkShopFinderImp extends CommonFinder<WorkShop> implements WorkShopFinder {
        private DetachedCriteria workShopCriteria;
        private WorkShopFinderImp(){
            this.workShopCriteria = detachedAll();
        }
        @Override
        public WorkShopFinder workShopUsed(boolean notUsed) {
            this.workShopCriteria.add(Restrictions.eq("notUsed",notUsed));
            return this;
        }

        @Override
        public WorkShopFinder name(String name) {
            if (name==null) return this;
            this.workShopCriteria.add(Restrictions.eq("workShopName",name));
            return this;
        }

        @Override
        public WorkShopFinder workShopId(Integer id) {
            if (id==null) return this;
            this.workShopCriteria.add(Restrictions.eq("id",id));
            return this;
        }

        @Override
        public Criteria getCriteria() {
            return this.workShopCriteria.getExecutableCriteria(getSession());
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return this.workShopCriteria;
        }
    }
}
